﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Tayninh2011
{
    public sealed partial class FrmTableNames : RootForm
    {
        private static readonly FrmTableNames frmTableNames=new FrmTableNames();
        //private List<List<String>> RootForm._lstLayersClf = new List<List<string>>();
        private List<String[]> defaultLayers = new List<string[]>();
        private ITnUtilitiesFile fileUtil = new TnUtilities();
        private ITnUtilComponent compUtil = new TnUtilities();
        //private String strXmlFileName = "layers";
        //private FrmCash frmCash=FrmCash.CallMe;
        //private static string[,] userInfo = null;

        //public static string[,] UserInfo
        //{
        //    get { return FrmTableNames.userInfo; }
        //    set { FrmTableNames.userInfo = value; }
        //}
        //public List<List<String>> RootForm._lstLayersClf
        //{
        //    get { return RootForm._lstLayersClf; }
        //    set { RootForm._lstLayersClf = value; }
        //}

        public static FrmTableNames CallMe
        {
            get
            {
                return frmTableNames;
            }
        }
        
        
        private FrmTableNames()
        {
            //MessageBox.Show("log frmTableNames 001");
            InitializeComponent();
            //MessageBox.Show("log frmTableNames 002");
            //initDefaultLayers();
            //RootForm._lstLayersClf = fileUtil.ReadLayersFormXmlFile("layers", "");
            //int len=RootForm._lstLayersClf[0].Count;//so lop thua
            //add so luong combobox = len
            //this.fpnThua.Controls.Add(compUtil.CbxLayers());
            //foreach(String t in RootForm._lstLayersClf[0])
            //{
            //    //this.
            //}
            
        }
        
        public new void Show()
        {
            if (isShown)
            {
                base.Show();
            }
            else
            {
                base.Show();
                
                isShown = true;
            }
            initForm();
        }
        private static bool isShown = false;

        static FrmTableNames()
        {

            frmTableNames.FormClosing += new FormClosingEventHandler(frm_FormClosing);

        }
        private static void frm_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
            isShown = false;

            frmTableNames.forClose();

            frmTableNames.Hide();
        }


        private void initForm()
        {
            if (System.IO.File.Exists(RootForm._strXmlLayersFileName + ".xml"))
            {
                RootForm._lstLayersClf = fileUtil.ReadLayersFormXmlFile(RootForm._strXmlLayersFileName, "");
            }
            else
            {
                fileUtil.CreateXmlFile("sde", defaultLayers, RootForm._strXmlLayersFileName, "");
                RootForm._lstLayersClf = fileUtil.ReadLayersFormXmlFile(RootForm._strXmlLayersFileName, "");
            }
            //RootForm._lstLayersClf theo thu tu
            //1.thua -- 2.duong -- 3.hem -- 4.thua_giadat
            int len = RootForm._lstLayersClf.Count;
            //MessageBox.Show(RootForm._lstLayersClf[0].Count.ToString());
            int numEachLayer = 0;
            int curThua = this.fpnThua.Controls.Count;
            int curDuong = this.fpnDuong.Controls.Count;
            int curHem = this.fpnHem.Controls.Count;
            int curThuaGiadat = this.fpnGiaDat.Controls.Count;
            List<Control.ControlCollection> ctrClc = new List<Control.ControlCollection>();

            ctrClc.Add(fpnThua.Controls);
            ctrClc.Add(fpnDuong.Controls);
            ctrClc.Add(fpnHem.Controls);
            ctrClc.Add(fpnGiaDat.Controls);
            int k = 0;
            for (int i = 0; i < ctrClc.Count; i++)
            {
                k = ctrClc[i].Count;
                for (int j = 0; j <k - 1; j++)
                {
                    ctrClc[i].RemoveAt(0);
                }
            }
            for(int i=0;i<len;i++)
            {
                //thua
                numEachLayer = RootForm._lstLayersClf[i].Count;
                
                
                for (int j = 0; j < numEachLayer; j++)
                {
                    if (j == 0)
                    {
                        ctrClc[i].Add(compUtil.CbxLayers(RootForm._arrUserInfo, RootForm._lstLayersClf[i][j], true));
                    }
                    else
                    {
                        ctrClc[i].Add(compUtil.CbxLayers(RootForm._arrUserInfo, RootForm._lstLayersClf[i][j]));
                    }
                }
            }
            //this.fpnThua.Controls.Add(compUtil.CbxLayers(this._arrUserInfo));
        }

        private void forClose()
        {
            List<string[]> layers = new List<string[]>();
            string t = String.Empty;
            for (int i = 0; i < frmTableNames.fpnThua.Controls.Count - 1; i++)
            {
                t = frmTableNames.fpnThua.Controls[i].Text;
                if (i == 0)
                {
                    RootForm.NameThua = t;
                }
                
                layers.Add(new String[] { "thua", t });
            }
            for (int i = 0; i < frmTableNames.fpnDuong.Controls.Count - 1; i++)
            {
                t = frmTableNames.fpnDuong.Controls[i].Text;
                if (i == 0)
                {
                    RootForm.NameDuong = t;
                }
                layers.Add(new String[] { "duong", t });
            }
            for (int i = 0; i < frmTableNames.fpnHem.Controls.Count - 1; i++)
            {
                t = frmTableNames.fpnHem.Controls[i].Text;
                if (i == 0)
                {
                    RootForm.NameHem = t;
                }
                layers.Add(new String[] { "hem", t });
            }
            for (int i = 0; i < frmTableNames.fpnGiaDat.Controls.Count - 1; i++)
            {
                t = frmTableNames.fpnGiaDat.Controls[i].Text;
                layers.Add(new String[] { "thua_giadat", t });
            }
            RootForm._lstLayers = layers;
            fileUtil.CreateXmlFile("sde", layers, RootForm._strXmlLayersFileName, "");
        }

        private void label5_Click(object sender, EventArgs e)
        {

        }


        private void btnThemThua_Click(object sender, EventArgs e)
        {
            this.fpnThua.Controls.Add(compUtil.CbxLayers(RootForm._arrUserInfo));
            //this.fpnThua.Controls.SetChildIndex(this.fpnBtnThua, this.fpnThua.Controls.Count - 1);
            //if (this.fpnThua.Controls.Count > 2)
            //{
            //    this.btnXoaThua.Enabled = true;
            //}
        }

        private void btnXoaThua_Click(object sender, EventArgs e)
        {
            this.fpnThua.Controls.RemoveAt(this.fpnThua.Controls.Count - 2);
            if (this.fpnThua.Controls.Count <= 2)
            {
                btnXoaThua.Enabled = false;
            }
        }

        private void btnThemDuong_Click(object sender, EventArgs e)
        {
            this.fpnDuong.Controls.Add(compUtil.CbxLayers(RootForm._arrUserInfo));
            //this.fpnDuong.Controls.SetChildIndex(this.fpnBtnDuong, this.fpnDuong.Controls.Count - 1);
            //if (this.fpnDuong.Controls.Count > 2)
            //{
            //    this.btnXoaDuong.Enabled = true;
            //}
        }

        private void btnXoaDuong_Click(object sender, EventArgs e)
        {
            this.fpnDuong.Controls.RemoveAt(this.fpnDuong.Controls.Count - 2);
            if (this.fpnDuong.Controls.Count <= 2)
            {
                btnXoaDuong.Enabled = false;
            }
        }

        private void btnThemHem_Click(object sender, EventArgs e)
        {
            this.fpnHem.Controls.Add(compUtil.CbxLayers(RootForm._arrUserInfo));
            //this.fpnHem.Controls.SetChildIndex(this.fpnBtnHem, this.fpnHem.Controls.Count - 1);
            //if (this.fpnHem.Controls.Count > 2)
            //{
            //    this.btnXoaHem.Enabled = true;
            //}
        }

        private void btnXoaHem_Click(object sender, EventArgs e)
        {
            this.fpnHem.Controls.RemoveAt(this.fpnHem.Controls.Count - 2);
            if (this.fpnHem.Controls.Count <= 2)
            {
                btnXoaHem.Enabled = false;
            }
        }

        private void btnThemGiaDat_Click(object sender, EventArgs e)
        {
            this.fpnGiaDat.Controls.Add(compUtil.CbxLayers(RootForm._arrUserInfo));
            //this.fpnGiaDat.Controls.SetChildIndex(this.fpnBtnGiadat, this.fpnGiaDat.Controls.Count - 1);
            //if (this.fpnGiaDat.Controls.Count > 2)
            //{
            //    this.btnXoaGiaDat.Enabled = true;
            //}
        }

        private void btnXoaGiaDat_Click(object sender, EventArgs e)
        {
            this.fpnGiaDat.Controls.RemoveAt(this.fpnGiaDat.Controls.Count - 2);
            if (this.fpnGiaDat.Controls.Count <= 2)
            {
                btnXoaGiaDat.Enabled = false;
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {

            this.Close();
        }

        private void fpnThua_ControlAdded(object sender, ControlEventArgs e)
        {
            int c = this.fpnThua.Controls.Count;
            if (c > 0)
            {
                this.fpnThua.Controls.SetChildIndex(this.fpnBtnThua, c - 1);
            }
            if (this.fpnThua.Controls.Count > 2)
            {
                this.btnXoaThua.Enabled = true;
            }
        }

        private void fpnDuong_ControlAdded(object sender, ControlEventArgs e)
        {
            int c = this.fpnDuong.Controls.Count;
            if (c > 0)
            {
                this.fpnDuong.Controls.SetChildIndex(this.fpnBtnDuong, c - 1);
            }
            if (this.fpnDuong.Controls.Count > 2)
            {
                this.btnXoaDuong.Enabled = true;
            }
        }

        private void fpnHem_ControlAdded(object sender, ControlEventArgs e)
        {
            int c = this.fpnHem.Controls.Count;
            if (c > 0)
            {
                this.fpnHem.Controls.SetChildIndex(this.fpnBtnHem, c - 1);
            }
            if (this.fpnHem.Controls.Count > 2)
            {
                this.btnXoaHem.Enabled = true;
            }
        }

        private void fpnGiaDat_ControlAdded(object sender, ControlEventArgs e)
        {
            int c = this.fpnGiaDat.Controls.Count;
            if (c > 0)
            {
                this.fpnGiaDat.Controls.SetChildIndex(this.fpnBtnGiadat, c - 1);
            }
            if (this.fpnGiaDat.Controls.Count > 2)
            {
                this.btnXoaGiaDat.Enabled = true;
            }
        }
    }
}
